package com.guigu.qingcheng.controller;

import com.guigu.common_util.pojo.utils.JwtUtils;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.qingcheng.entity.TbAddress;
import com.guigu.qingcheng.entity.TbOrder;
import com.guigu.qingcheng.service.CartService;
import com.guigu.qingcheng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private UserService userService;

    @GetMapping("cart/findCartList")
    @ResponseBody
    public List<Map<String,Object>> findCartList(HttpServletRequest request) {
        String username = JwtUtils.getMemberIdByJwtToken2(request);
        List<Map<String, Object>> cartList = cartService.findCartList(username);
        return cartList;
    }


    /**
     * 删除选中的购物车
     */
    @GetMapping("cart/deleteCheckedCart")
    @ResponseBody
    public Result deleteCheckedCart(HttpServletRequest request){
        String username = JwtUtils.getMemberIdByJwtToken2(request);
        cartService.deleteCheckedCart(username);
        return Result.ok();
    }


    /**
     * 计算当前选中的购物车的优惠金额
     * @param
     * @return
     */
    @GetMapping("cart/preferential")
    @ResponseBody
    public Map preferential(HttpServletRequest request){
        String username = JwtUtils.getMemberIdByJwtToken2(request);
        return cartService.preferential(username);
    }

    /**
     * 获取刷新单价后的购物车列表
     * @return
     */
    @GetMapping("cart/findNewOrderItemList")
    @ResponseBody
    public List<Map<String, Object>> findNewOrderItemList(HttpServletRequest request){
        String username = JwtUtils.getMemberIdByJwtToken2(request);
        return cartService.findNewOrderItemList(username);
    }


    @GetMapping("cart/addItem")
    @ResponseBody
    public Result addItem(HttpServletRequest request,String skuId,Integer num){
        String username = JwtUtils.getMemberIdByJwtToken2(request);
        cartService.addItem(username,skuId,num);
        return Result.ok();
    }

    /**
     * 更改购物车项选中状态
     * @param skuId
     * @param checked
     * @return
     */
    @GetMapping("/cart/updateChecked")
    @ResponseBody
    public Result updateChecked(HttpServletRequest request,String skuId, boolean checked){
        String username = JwtUtils.getMemberIdByJwtToken2(request);
        cartService.updateChecked(username,skuId,checked);
        return Result.ok();
    }

    /**
     * 根据用户名查询地址列表
     * @return
     */
    @RequestMapping("cart/findAddressList")
    @ResponseBody
    public List<TbAddress> findAddressList(HttpServletRequest request){
        String username = JwtUtils.getMemberIdByJwtToken2(request);
        return userService.findAddressList(username);
    }

    /**
     * 保存订单
     * @param order
     * @return
     */
    @PostMapping("cart/saveOrder")
    @ResponseBody
    public Map<String,Object> saveOrder(HttpServletRequest request,@RequestBody TbOrder order ){
        String username= JwtUtils.getMemberIdByJwtToken2(request);
        order.setUsername(username);
        return cartService.saveOrder(order,username);
    }


    @GetMapping("buy")
    public String buy(HttpServletRequest request,String skuId, Integer num){
        String username = JwtUtils.getMemberIdByJwtToken2(request);
        if (username != "" && !"".equals(username)){
            cartService.addItem(username,skuId,num);
            return "cart";
        }
        return "login";
    }

    @GetMapping("cart")
    private String index(){
        return "cart";
    }

    @GetMapping("order")
    private String order() {
        return "order";
    }

    @GetMapping("pay")
    public String pay(String ordersn, Integer money, Model model){
        model.addAttribute("ordersn",ordersn);
        model.addAttribute("money",money);
        return "pay";
    }

    @GetMapping("ordersuccess")
    public String paysuccess(String ordersn,Integer money, Model model) {
        model.addAttribute("ordersn",ordersn);
        model.addAttribute("money",money);
        return "order-success";
    }

    @GetMapping("orderfail")
    public String payfail(Model model) {
        return "order-fail";
    }

    @GetMapping("weixinpay")
    public String weixinpay(Model model,String orderId){
        model.addAttribute("orderId",orderId);
        return "weixinpay";
    }

    @GetMapping("paysuccess")
    public String paysuccess(Model model,Integer money){
        model.addAttribute("money",money);
        return "paysuccess";
    }

}
